package com.iap.common.view.control.code
{
	import com.iap.app.util.BMPFunctions;
	import com.iap.common.model.Filler;
	import com.iap.common.model.VectorImage;
	import com.iap.common.view.control.VectorImageFillerDisplay;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.BlendMode;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.events.Event;
	import flash.net.URLRequest;
	
	import spark.components.Group;
	import spark.components.Image;
	
	public class VectorImageFillerDisplayCode extends Group
	{
		private static const DEFAULT_IMAGE_WIDTH:uint = 500;

		protected var view:VectorImageFillerDisplay;
		
		public var model:VectorImage;
		
		private var originalBitmap:BitmapData;
		
		public var filler:Filler;
		
		private var imageWidth:uint = DEFAULT_IMAGE_WIDTH;
		
		public var ready:Boolean = false;
		
		public function VectorImageFillerDisplayCode()
		{
			super();
		}

		public function setSource(model:VectorImage, filler:Filler = null):void
		{
			this.model = model;
			this.filler = filler;
			this.imageWidth = width > 0 ? width : DEFAULT_IMAGE_WIDTH;
		}
		
		protected function init(view:VectorImageFillerDisplay):void
		{
			this.view = view;
			initImage();
		}
		
		private function initImage():void
		{
			if(model == null) return;
			
			var loader:Loader = new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoadCompleteHandler);
			loader.load(new URLRequest(model.image.getThumb(imageWidth)));
		}
		
		private function imageLoadCompleteHandler(evt:Event):void
		{
			var loader:Loader = (evt.target as LoaderInfo).loader;
			loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, imageLoadCompleteHandler);
			
			originalBitmap = new BitmapData(loader.content.width,loader.content.height, true, 0x000000);
			originalBitmap.draw(loader.content);
			
			if(filler != null)
			{
				setFiller(filler);
			} else {
				addOriginalImage();
			}
			dispatchReadyEvent();
		}
		
		private function dispatchReadyEvent():void
		{
			dispatchEvent(new Event(VectorImageFillerDisplay.EVENT_READY));
		}
		
		private function addOriginalImage():void
		{
			var bitmap:Bitmap = new Bitmap(originalBitmap);
			var image:Image = new Image();
			image.source = bitmap;
			view.removeAllElements();
			view.addElement(image);
		}
		
		private function changeColorBitmapData(bitmapData:BitmapData, newColor:uint):Image
		{
			var replacedColor:uint = 0xFFFFFFFF;
			
			var tolerance:uint = Math.max(0, Math.min(255, 100));
			var bitmapReplaced:BitmapData = BMPFunctions.replaceColor(bitmapData, replacedColor, newColor, tolerance);
			var bitmap:Bitmap = new Bitmap(bitmapReplaced);
			
			var image:Image = new Image();
			image.source = bitmap;
			return image;
		}
		
		public function setFiller(filler:Filler):void
		{
			switch(filler.type)
			{
				case Filler.TYPE_SOLID:
				{
					setFillerSolid(filler);
					break;
				}
				case Filler.TYPE_TEXTURE:
				{
					setFillerTexture(filler);
					break;
				}
			}
		}
		
		private function setFillerSolid(filler:Filler):void
		{
			if(filler.type == Filler.TYPE_TEXTURE) return;
			var argb:uint = convertStringToARGB(filler.value, 255);
			var image:Image = changeColorBitmapData(originalBitmap.clone(), argb);
			view.removeAllElements();
			view.addElement(image);
		}
		
		private function setFillerTexture(filler:Filler):void
		{
			if(filler.type == Filler.TYPE_SOLID) return;
			loadTexture(filler);
		}
		
		private function convertStringToARGB(color:String, newAlpha:uint):uint
		{
			var rgb:uint = uint(color);
			//newAlpha has to be in the 0 to 255 range
			var argb:uint = 0;
			argb += (newAlpha<<24);
			argb += (rgb);
			return argb;
		}
		
		private function loadTexture(filler:Filler):void
		{
			var loader:Loader = new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, textureLoadCompleteHandler);
			loader.load(new URLRequest(filler.image.url));
		}
		
		private function textureLoadCompleteHandler(evt:Event):void
		{
			var loader:Loader = (evt.target as LoaderInfo).loader;
			loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, textureLoadCompleteHandler);
			
			var txtBitmap:BitmapData = new BitmapData(loader.content.width,loader.content.height, false);
			txtBitmap.draw(loader.content);
			initTexture(txtBitmap);
		}
		
		private function initTexture(txtBitmap:BitmapData):void
		{
			var parentGroup:Group = new Group();
			
			var topImage:Image = new Image();
			topImage.source = new Bitmap(originalBitmap);
			
			var maskImage:Image = new Image();
			maskImage.source = new Bitmap(originalBitmap);
			
			
			var image:Image = new Image();
			image.graphics.beginBitmapFill(txtBitmap, null, true, true);
			image.graphics.drawRect(0,0, view.width, view.height);
			
			view.removeAllElements();
			view.addElement(parentGroup);
			parentGroup.addElement(image);
			parentGroup.addElement(maskImage);
			parentGroup.addElement(topImage);
			
			parentGroup.blendMode = BlendMode.LAYER;
			maskImage.blendMode = BlendMode.ALPHA;
			topImage.blendMode = BlendMode.DARKEN;
		}
	}
}